﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;

namespace DMZS.DAL
{
    /// <summary>
    /// 数据访问类:PType
    /// </summary>
    public partial class PType
    {
        public PType()
        {
        }

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("PtypeFID", "PType");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int PtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from PType");
            strSql.Append(" where PtypeFID=@PtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@PtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = PtypeFID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DMZS.Model.PType model)
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into PType(");
            strSql.Append("ParID,PTypeID,Leveal,PSonnum,PSonCount,PCode,pFullName,pName,pNamePY,pTypeGG,pModel,IsStop,pComment,UnitID,UnitID1,Change,pUserCode)");
            strSql.Append(" values (");
            strSql.Append("@ParID,@PTypeID,@Leveal,@PSonnum,@PSonCount,@PCode,@pFullName,@pName,@pNamePY,@pTypeGG,@pModel,@IsStop,@pComment,@UnitID,@UnitID1,@Change,@pUserCode)");
            strSql.Append(";select @@IDENTITY");
            var parameters = new SqlParameter[] { new SqlParameter("@ParID", SqlDbType.VarChar, 25),
                    new SqlParameter("@PTypeID", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@PSonnum", SqlDbType.Int, 4),
                    new SqlParameter("@PSonCount", SqlDbType.Int, 4),
                    new SqlParameter("@PCode", SqlDbType.VarChar, 100),
                    new SqlParameter("@pFullName", SqlDbType.VarChar, 200),
                    new SqlParameter("@pName", SqlDbType.VarChar, 100),
                    new SqlParameter("@pNamePY", SqlDbType.VarChar, 66),
                    new SqlParameter("@pTypeGG", SqlDbType.VarChar, 100),
                    new SqlParameter("@pModel", SqlDbType.VarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Bit, 1),
                    new SqlParameter("@pComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@UnitID", SqlDbType.Int, 4),
                    new SqlParameter("@UnitID1", SqlDbType.Int, 4),
                    new SqlParameter("@Change", SqlDbType.Int, 4),
                    new SqlParameter("@pUserCode", SqlDbType.VarChar, 100) };
            parameters[0].Value = model.ParID;
            parameters[1].Value = model.PTypeID;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.PSonnum;
            parameters[4].Value = model.PSonCount;
            parameters[5].Value = model.PCode;
            parameters[6].Value = model.pFullName;
            parameters[7].Value = model.pName;
            parameters[8].Value = model.pNamePY;
            parameters[9].Value = model.pTypeGG;
            parameters[10].Value = model.pModel;
            parameters[11].Value = model.IsStop;
            parameters[12].Value = model.pComment;
            parameters[13].Value = model.UnitID;
            parameters[14].Value = model.UnitID1;
            parameters[15].Value = model.Change;
            parameters[16].Value = model.pUserCode;

            var obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DMZS.Model.PType model)
        {
            var strSql = new StringBuilder();
            strSql.Append("update PType set ");
            strSql.Append("ParID=@ParID,");
            strSql.Append("PTypeID=@PTypeID,");
            strSql.Append("Leveal=@Leveal,");
            strSql.Append("PSonnum=@PSonnum,");
            strSql.Append("PSonCount=@PSonCount,");
            strSql.Append("PCode=@PCode,");
            strSql.Append("pFullName=@pFullName,");
            strSql.Append("pName=@pName,");
            strSql.Append("pNamePY=@pNamePY,");
            strSql.Append("pTypeGG=@pTypeGG,");
            strSql.Append("pModel=@pModel,");
            strSql.Append("IsStop=@IsStop,");
            strSql.Append("pComment=@pComment,");
            strSql.Append("UnitID=@UnitID,");
            strSql.Append("UnitID1=@UnitID1,");
            strSql.Append("Change=@Change,");
            strSql.Append("pUserCode=@pUserCode");
            strSql.Append(" where PtypeFID=@PtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@ParID", SqlDbType.VarChar, 25),
                    new SqlParameter("@PTypeID", SqlDbType.VarChar, 25),
                    new SqlParameter("@Leveal", SqlDbType.Int, 4),
                    new SqlParameter("@PSonnum", SqlDbType.Int, 4),
                    new SqlParameter("@PSonCount", SqlDbType.Int, 4),
                    new SqlParameter("@PCode", SqlDbType.VarChar, 100),
                    new SqlParameter("@pFullName", SqlDbType.VarChar, 200),
                    new SqlParameter("@pName", SqlDbType.VarChar, 100),
                    new SqlParameter("@pNamePY", SqlDbType.VarChar, 66),
                    new SqlParameter("@pTypeGG", SqlDbType.VarChar, 100),
                    new SqlParameter("@pModel", SqlDbType.VarChar, 100),
                    new SqlParameter("@IsStop", SqlDbType.Bit, 1),
                    new SqlParameter("@pComment", SqlDbType.VarChar, 256),
                    new SqlParameter("@UnitID", SqlDbType.Int, 4),
                    new SqlParameter("@UnitID1", SqlDbType.Int, 4),
                    new SqlParameter("@Change", SqlDbType.Int, 4),
                    new SqlParameter("@pUserCode", SqlDbType.VarChar, 100),
                    new SqlParameter("@PtypeFID", SqlDbType.Int, 4) };
            parameters[0].Value = model.ParID;
            parameters[1].Value = model.PTypeID;
            parameters[2].Value = model.Leveal;
            parameters[3].Value = model.PSonnum;
            parameters[4].Value = model.PSonCount;
            parameters[5].Value = model.PCode;
            parameters[6].Value = model.pFullName;
            parameters[7].Value = model.pName;
            parameters[8].Value = model.pNamePY;
            parameters[9].Value = model.pTypeGG;
            parameters[10].Value = model.pModel;
            parameters[11].Value = model.IsStop;
            parameters[12].Value = model.pComment;
            parameters[13].Value = model.UnitID;
            parameters[14].Value = model.UnitID1;
            parameters[15].Value = model.Change;
            parameters[16].Value = model.pUserCode;
            parameters[17].Value = model.PtypeFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int PtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from PType ");
            strSql.Append(" where PtypeFID=@PtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@PtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = PtypeFID;

            var rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string PtypeFIDlist )
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from PType ");
            strSql.Append(" where PtypeFID in (" + PtypeFIDlist + ")  ");
            var rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.PType GetModel(int PtypeFID)
        {
            var strSql = new StringBuilder();
            strSql.Append("select  top 1 PtypeFID,ParID,PTypeID,Leveal,PSonnum,PSonCount,PCode,pFullName,pName,pNamePY,pTypeGG,pModel,IsStop,pComment,UnitID,UnitID1,Change,pUserCode from PType ");
            strSql.Append(" where PtypeFID=@PtypeFID");
            var parameters = new SqlParameter[] { new SqlParameter("@PtypeFID", SqlDbType.Int, 4)
            };
            parameters[0].Value = PtypeFID;
            var ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DMZS.Model.PType DataRowToModel(DataRow row)
        {
            var model = new DMZS.Model.PType();
            if (row != null)
            {
                if (row["PtypeFID"] != null && row["PtypeFID"].ToString() != string.Empty)
                {
                    model.PtypeFID = int.Parse(row["PtypeFID"].ToString());
                }
                if (row["ParID"] != null)
                {
                    model.ParID = row["ParID"].ToString();
                }
                if (row["PTypeID"] != null)
                {
                    model.PTypeID = row["PTypeID"].ToString();
                }
                if (row["Leveal"] != null && row["Leveal"].ToString() != string.Empty)
                {
                    model.Leveal = int.Parse(row["Leveal"].ToString());
                }
                if (row["PSonnum"] != null && row["PSonnum"].ToString() != string.Empty)
                {
                    model.PSonnum = int.Parse(row["PSonnum"].ToString());
                }
                if (row["PSonCount"] != null && row["PSonCount"].ToString() != string.Empty)
                {
                    model.PSonCount = int.Parse(row["PSonCount"].ToString());
                }
                if (row["PCode"] != null)
                {
                    model.PCode = row["PCode"].ToString();
                }
                if (row["pFullName"] != null)
                {
                    model.pFullName = row["pFullName"].ToString();
                }
                if (row["pName"] != null)
                {
                    model.pName = row["pName"].ToString();
                }
                if (row["pNamePY"] != null)
                {
                    model.pNamePY = row["pNamePY"].ToString();
                }
                if (row["pTypeGG"] != null)
                {
                    model.pTypeGG = row["pTypeGG"].ToString();
                }
                if (row["pModel"] != null)
                {
                    model.pModel = row["pModel"].ToString();
                }
                if (row["IsStop"] != null && row["IsStop"].ToString() != string.Empty)
                {
                    if ((row["IsStop"].ToString() == "1") || (row["IsStop"].ToString().ToLower() == "true"))
                    {
                        model.IsStop = true;
                    }
                    else
                    {
                        model.IsStop = false;
                    }
                }
                if (row["pComment"] != null)
                {
                    model.pComment = row["pComment"].ToString();
                }
                if (row["UnitID"] != null && row["UnitID"].ToString() != string.Empty)
                {
                    model.UnitID = int.Parse(row["UnitID"].ToString());
                }
                if (row["UnitID1"] != null && row["UnitID1"].ToString() != string.Empty)
                {
                    model.UnitID1 = int.Parse(row["UnitID1"].ToString());
                }
                if (row["Change"] != null && row["Change"].ToString() != string.Empty)
                {
                    model.Change = int.Parse(row["Change"].ToString());
                }
                if (row["pUserCode"] != null)
                {
                    model.pUserCode = row["pUserCode"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select PtypeFID,ParID,PTypeID,Leveal,PSonnum,PSonCount,PCode,pFullName,pName,pNamePY,pTypeGG,pModel,IsStop,pComment,UnitID,UnitID1,Change,pUserCode ");
            strSql.Append(" FROM PType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" PtypeFID,ParID,PTypeID,Leveal,PSonnum,PSonCount,PCode,pFullName,pName,pNamePY,pTypeGG,pModel,IsStop,pComment,UnitID,UnitID1,Change,pUserCode ");
            strSql.Append(" FROM PType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) FROM PType ");
            if (strWhere.Trim() != string.Empty)
            {
                strSql.Append(" where " + strWhere);
            }
            var obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            var strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby );
            }
            else
            {
                strSql.Append("order by T.PtypeFID desc");
            }
            strSql.Append(")AS Row, T.*  from PType T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}

